package com.ruoyi.system.controller;

import com.alibaba.fastjson.JSON;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.WebUser;
import com.ruoyi.system.domain.WebUserResultFollow;
import com.ruoyi.system.service.IWebUserResultFollowService;
import com.ruoyi.system.service.IWebUserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 效果跟进Controller
 * 
 * @author wsliupan@126.com
 * @date 2021-07-02
 */
@Controller
@RequestMapping("/system/userResultFollow")
public class WebUserResultFollowController extends BaseController
{
    private String prefix = "system/userResultFollow";

    @Autowired
    private IWebUserResultFollowService webUserResultFollowService;
    @Autowired
    private IWebUserService iWebUserService;

    @RequiresPermissions("system:userResultFollow:view")
    @GetMapping()
    public String userResultFollow()
    {
        return prefix + "/userResultFollow";
    }

    /**
     * 查询效果跟进列表
     */
    @RequiresPermissions("system:userResultFollow:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(WebUserResultFollow webUserResultFollow)
    {
        startPage();
        List<WebUserResultFollow> list = webUserResultFollowService.selectWebUserResultFollowList(webUserResultFollow);
        return getDataTable(list);
    }

    /**
     * 导出效果跟进列表
     */
    @RequiresPermissions("system:userResultFollow:export")
    @Log(title = "效果跟进", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(WebUserResultFollow webUserResultFollow)
    {
        List<WebUserResultFollow> list = webUserResultFollowService.selectWebUserResultFollowList(webUserResultFollow);
        ExcelUtil<WebUserResultFollow> util = new ExcelUtil<WebUserResultFollow>(WebUserResultFollow.class);
        return util.exportExcel(list, "效果跟进数据");
    }

    /**
     * 新增效果跟进
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存效果跟进
     */
    @RequiresPermissions("system:userResultFollow:add")
    @Log(title = "效果跟进", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(WebUserResultFollow webUserResultFollow)
    {
        return toAjax(webUserResultFollowService.insertWebUserResultFollow(webUserResultFollow));
    }

    /**
     * 修改效果跟进
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        WebUserResultFollow webUserResultFollow = null;
        if(id == null || id ==  -1){
            webUserResultFollow = new WebUserResultFollow();
        }else{
            webUserResultFollow = webUserResultFollowService.selectWebUserResultFollowById(id);
            webUserResultFollow.setRealname((webUserResultFollow.getNickname()==null?"":webUserResultFollow.getNickname())+"-"+(webUserResultFollow.getRealname()==null?"":webUserResultFollow.getRealname()));
        }
        mmap.put("webUserResultFollow", webUserResultFollow);

        WebUser user = new WebUser();
        List<WebUser> users = iWebUserService.selectWebUserList(user);
        mmap.put("users", JSON.toJSONString(users));

        return prefix + "/edit";
    }

    /**
     * 修改保存效果跟进
     */
    @RequiresPermissions("system:userResultFollow:edit")
    @Log(title = "效果跟进", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(WebUserResultFollow webUserResultFollow)
    {
        if(webUserResultFollow.getUserId() != null){
            WebUser user = iWebUserService.getById(webUserResultFollow.getUserId());
            webUserResultFollow.setDeptId(user.getDeptId());
        }
        return toAjax(webUserResultFollowService.updateWebUserResultFollow(webUserResultFollow));
    }

    /**
     * 删除效果跟进
     */
    @RequiresPermissions("system:userResultFollow:remove")
    @Log(title = "效果跟进", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(webUserResultFollowService.deleteWebUserResultFollowByIds(ids));
    }
}
